package com.example.graduatetest.view.model;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.graduatetest.R;
import com.example.graduatetest.callback.StringCallBack;
import com.example.graduatetest.databinding.ActivityChangePasswordBinding;
import com.example.graduatetest.model.StudentModel;
import com.example.graduatetest.model.TeacherModel;

public class ChangePasswordViewModel extends ViewModel {
    private Activity activity;
    private ActivityChangePasswordBinding binding;
    private MutableLiveData<String> oldPassword;
    private MutableLiveData<String> newPassword;
    private MutableLiveData<String> newPasswordAgain;
    //默认修改密码失败
    private String changePasswordResult = "fail";
    private Handler handler = new Handler();

    public void init(Activity activity,ActivityChangePasswordBinding binding){
        this.activity = activity;
        this.binding = binding;
        //创建，使其不为空
        if(oldPassword == null) oldPassword = new MutableLiveData<>();
        if(newPassword == null) newPassword = new MutableLiveData<>();
        if(newPasswordAgain == null) newPasswordAgain = new MutableLiveData<>();

        toolbarInit();
    }

    //初始化toolbar
    private void toolbarInit(){
        Toolbar toolbar = binding.toolbar6;
        toolbar.setTitle("修改密码");
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });
    }

    //设置点击事件
    public void submitClick(){


        SharedPreferences preferences = activity.getSharedPreferences("data", Context.MODE_PRIVATE);
        boolean isStudent = preferences.getBoolean("isStudent", true);
        String number = preferences.getString("number", "2017211973");
        String password = preferences.getString("password", "默认");

        if(oldPassword.getValue() != null && newPasswordAgain.getValue() != null && newPassword.getValue() != null){
            if(!password.equals(oldPassword.getValue())){
                Toast.makeText(activity, "原密码错误密码", Toast.LENGTH_SHORT).show();
            }else if(!newPassword.getValue().equals(newPasswordAgain.getValue())){
                Toast.makeText(activity, "两次密码不一致", Toast.LENGTH_SHORT).show();
            }else if(oldPassword.getValue().equals(newPassword.getValue())){
                Toast.makeText(activity, "新旧密码一致，不必修改", Toast.LENGTH_SHORT).show();
            }else{
                Log.e("haha", "修改成功");
                //修改密码逻辑
                changePassword(number, newPassword.getValue(), isStudent);
            }
        }else{
            Toast.makeText(activity, "新、旧密码不能为空", Toast.LENGTH_SHORT).show();
        }
    }

    //修改密码逻辑
    private void changePassword(String number,String password,boolean isStudent){
        if(isStudent){
            StudentModel.changePassword(number, password, new StringCallBack() {
                @Override
                public void onSuccess(String result) {
                    changePasswordResult = result;
                }

                @Override
                public void onFailed() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(activity, "密码修改失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        }else{
            TeacherModel.changePassword(number, password, new StringCallBack() {
                @Override
                public void onSuccess(String result) {
                    changePasswordResult = result;
                }

                @Override
                public void onFailed() {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(activity, "密码修改失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        }
        if(changePasswordResult.equals("success")){
            SharedPreferences.Editor editor = activity.getSharedPreferences("data", Context.MODE_PRIVATE).edit();
            editor.putString("password", password);
            editor.apply();
            Toast.makeText(activity, "密码修改成功", Toast.LENGTH_SHORT).show();
            Log.e("haha","修改成功");
            activity.onBackPressed();
        }

    }

    public MutableLiveData<String> getNewPassword() {
        return newPassword;
    }

    public MutableLiveData<String> getNewPasswordAgain() {
        return newPasswordAgain;
    }

    public MutableLiveData<String> getOldPassword() {
        return oldPassword;
    }
}
